package com.wedimob.slideshare.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wedimob.slideshare.bo.Container;

public class Results {

    private Map<Class<?>, List<Object>> resultMapCounter = new HashMap<Class<?>, List<Object>>();

    public synchronized int getNbResultType(Class<?> c) {
        List<Object> list = resultMapCounter.get(c);
        if (list != null)
            return list.size();

        return 0;
    }

    @SuppressWarnings("unchecked")
    public synchronized <T> List<T> getResultByType(Class<T> c) {
        return (List<T>) resultMapCounter.get(c);
    }

    public synchronized void putResult(Object output) {
        if (output == null)
            return;

        if (output instanceof Container) {
            Container<?> container = (Container<?>) output;
            for (Object o : container) {
                if (!resultMapCounter.containsKey(o.getClass())) {
                    resultMapCounter.put(o.getClass(), new ArrayList<Object>());
                }
                resultMapCounter.get(o.getClass()).add(o);
            }
            return;
        }

        if (!resultMapCounter.containsKey(output.getClass())) {
            resultMapCounter.put(output.getClass(), new ArrayList<Object>());
        }
        resultMapCounter.get(output.getClass()).add(output);
    }
}
